package com.m3u8.db;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;

import com.m3u8.DownloadConstats;
import com.m3u8.db.model.M3u8Model;
import com.m3u8.db.model.TsModel;

@Database(entities = {M3u8Model.class, TsModel.class},
        version = DownloadConstats.DB.DB_VERSION, exportSchema = false)
@TypeConverters({Converters.class})
public abstract class DownloadM3u8DB extends RoomDatabase {

    private static DownloadM3u8DB instance;
    private static final byte[] lock = new byte[0];

    public static DownloadM3u8DB getInstance(Context context) {
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = Room.databaseBuilder(context.getApplicationContext(), DownloadM3u8DB.class, DownloadConstats.DB.DB_NAME)
                            .allowMainThreadQueries()
                            .fallbackToDestructiveMigration()//迁移数据库如果发生错误，将会重新创建数据库，而不是发生崩溃
                            .build();
                }
            }
        }
        return instance;
    }

    public abstract M3u8Dao m3u8Dao();

    public abstract TsDao tsDao();

    public void closeDb() {
        instance.close();
        instance = null;
    }
}
